/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/math.h"

/**
 * @brief Calculates the cosine of a double using a series expansion.
 *
 * This function returns the cosine of the given double value using a series expansion.
 * It stops when the term is smaller than the defined epsilon.
 *
 * @param x The double value.
 * @return double The cosine of x.
 */
double Scos(double x) {
    double result = 1.0;  
    double term = 1.0;  
    int n = 0;  

    while (Sabsolute(term) > DEPSILON) {
        n++;  
        term *= -x * x / (2 * n * (2 * n - 1));
        result += term;  
    }  
    return result;  
}
